package com.ppblock.shirox.thymeleaf.processor;

import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.exceptions.TemplateProcessingException;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.standard.expression.IStandardExpression;
import org.thymeleaf.standard.expression.StandardExpressionParser;
import org.thymeleaf.util.EvaluationUtils;
import org.thymeleaf.util.StringUtils;
import org.thymeleaf.util.Validate;

import java.util.*;

/**
 * @author yangjian
 * @since 2017/12/12
 */
public final class ThymeleafFacade {
    private ThymeleafFacade() {
        throw new UnsupportedOperationException();
    }

    public static String getRawValue(IProcessableElementTag element, AttributeName attributeName) {
        Validate.notNull(element, "element must not be null");
        Validate.notNull(attributeName, "attributeName must not be empty");
        String rawValue = StringUtils.trim(element.getAttributeValue(attributeName));
        Validate.notEmpty(rawValue, "value of \'" + attributeName + "\' must not be empty");
        return rawValue;
    }

    public static String getRawValue(IProcessableElementTag element, String attributeName) {
        Validate.notNull(element, "element must not be null");
        Validate.notEmpty(attributeName, "attributeName must not be empty");
        String rawValue = StringUtils.trim(element.getAttributeValue(attributeName));
        Validate.notEmpty(rawValue, "value of \'" + attributeName + "\' must not be empty");
        return rawValue;
    }

    public static Object evaluateExpression(ITemplateContext arguments, String expression) throws TemplateProcessingException {
        Validate.notNull(arguments, "arguments must not be null");
        Validate.notEmpty(expression, "expression must not be empty");
        StandardExpressionParser parser = new StandardExpressionParser();
        IStandardExpression evaluableExpression = parser.parseExpression(arguments, expression);
        return evaluableExpression.execute(arguments);
    }

    public static List<Object> evaluateAsIterable(ITemplateContext arguments, String rawValue) throws TemplateProcessingException {
        Validate.notNull(arguments, "arguments must not be null");
        Validate.notEmpty(rawValue, "rawValue must not be empty");
        Object evaluatedExpression = evaluateExpression(arguments, rawValue);
        return EvaluationUtils.evaluateAsList(evaluatedExpression);
    }

    public static List<Object> evaluateAsIterableOrRawValue(ITemplateContext arguments, String rawValue) {
        Validate.notNull(arguments, "arguments must not be null");
        Validate.notEmpty(rawValue, "rawValue must not be empty");
        ArrayList result = new ArrayList();

        try {
            result.addAll(evaluateAsIterable(arguments, rawValue));
        } catch (TemplateProcessingException var4) {
            result.add(rawValue);
        }

        return Collections.unmodifiableList(result);
    }

    public static List<String> evaluateAsStringsWithDelimiter(ITemplateContext arguments, String rawValue, String delimiter) {
        Validate.notNull(arguments, "arguments must not be null");
        Validate.notEmpty(rawValue, "rawValue must not be empty");
        Validate.notEmpty(delimiter, "delimiter must not be empty");
        ArrayList result = new ArrayList();
        List iterates = evaluateAsIterableOrRawValue(arguments, rawValue);
        Iterator var5 = iterates.iterator();

        while(var5.hasNext()) {
            Object o = var5.next();
            result.addAll(Arrays.asList(StringUtils.split(o, delimiter)));
        }

        return Collections.unmodifiableList(result);
    }
}
